require("prefabutil")

local prefabs =
{
    "collapse_small",
}

local function MakeLight(name)
    local assets =
    {
        Asset("ANIM", "anim/" .. name .. ".zip"),
    }

    -- 根据当前世界状态，决定灯光是开还是关
    local function RecalcLightPhase(inst)
        if TheWorld:HasTag("cave") then
            inst.Light:Enable(true)
            if inst.halo == nil then
                inst.halo = SpawnPrefab(name .. "_halo")
                local x, y, z = inst.Transform:GetWorldPosition()
                inst.halo.Transform:SetPosition(x, y, z)
                inst.halo.persists = false -- 不保存,重载游戏后消失
            end
            inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
        else
            local phase = TheWorld.state.phase
            if phase == "day" then
                inst.Light:Enable(false)
                if inst.halo then
                    inst.halo:Remove()
                    inst.halo = nil
                end
                inst.AnimState:ClearBloomEffectHandle()
            else
                inst.Light:Enable(true)
                if inst.halo == nil then
                    inst.halo = SpawnPrefab(name .. "_halo")
                    local x, y, z = inst.Transform:GetWorldPosition()
                    inst.halo.Transform:SetPosition(x, y, z)
                    inst.halo.persists = false
                end
                inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
            end
        end
    end

    local function onworkfinished(inst)
        inst.components.lootdropper:DropLoot()
        local fx = SpawnPrefab("collapse_small")
        fx.Transform:SetPosition(inst.Transform:GetWorldPosition())
        fx:SetMaterial("wood")
        inst:Remove()
    end

    local function fn()
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddSoundEmitter()
        inst.entity:AddMiniMapEntity()
        inst.entity:AddLight()
        inst.entity:AddNetwork()

        inst:SetDeploySmartRadius(0.5)

        MakeObstaclePhysics(inst, .4)

        inst.MiniMapEntity:SetIcon(name .. ".png")

        inst.AnimState:SetBank(name)
        inst.AnimState:SetBuild(name)
        inst.AnimState:PlayAnimation("idle")
        inst.AnimState:Hide("halo")

        if name == "bboy_candle_lamp" then
            inst.Light:SetColour(1, 0.5, 0) -- 橙色光
        elseif name == "bboy_nether_lamp" then
            inst.Light:SetColour(0, 0.5, 1) -- 蓝色光
        end
        inst.Light:SetRadius(5)             -- 设置光照范围
        inst.Light:SetFalloff(0.9)          -- 设置光照衰减率
        inst.Light:SetIntensity(0.6)        -- 设置光照强度
        inst.Light:Enable(true)

        inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh") -- 加个泛光,游戏中可以在官方设置中开启关闭

        inst:AddTag("structure")
        inst:AddTag("lamp")
        inst:AddTag("decortable")
        if name == "bboy_nether_lamp" then
            inst:AddTag("bboy_nether_lamp")
        end

        MakeSnowCoveredPristine(inst)

        inst.entity:SetPristine()

        if not TheWorld.ismastersim then
            return inst
        end

        MakeHauntableWork(inst)
        MakeSnowCovered(inst)

        inst:AddComponent("workable")
        inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
        inst.components.workable:SetWorkLeft(2)
        inst.components.workable:SetOnFinishCallback(onworkfinished)

        inst:AddComponent("inspectable")

        inst:AddComponent("lootdropper")

        if name == "bboy_nether_lamp" then
            inst:AddComponent("bboy_touch")
        end

        inst:WatchWorldState("phase", RecalcLightPhase)
        RecalcLightPhase(inst)

        return inst
    end

    return Prefab(name, fn, assets, prefabs)
end

return MakeLight("bboy_candle_lamp"),
    MakeLight("bboy_nether_lamp"),
    MakePlacer("bboy_candle_lamp_placer", "bboy_candle_lamp", "bboy_candle_lamp", "idle"),
    MakePlacer("bboy_nether_lamp_placer", "bboy_nether_lamp", "bboy_nether_lamp", "idle")
